package com.example.userclient

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.utils.DistanceUtil
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.databinding.ListBookItemBinding
import java.text.DecimalFormat


object SaleBookListAdapter :
    RecyclerView.Adapter<SaleBookListAdapter.ViewHolder>() {
     var dataSet: Array<AddBook>? = null
    var format: DecimalFormat = DecimalFormat("0.##")
    var listener: OnItemClickListener? = null
    var isSelect = false
    fun clean(){

        SaleBookListAdapter.dataSet = ClientInfoHolder.liveBook?.toTypedArray()
        SaleBookListAdapter.notifyDataSetChanged()
        isSelect = false
    }

    fun filterByQuery(query: String?){
        isSelect = true
        if (query!=null){
            dataSet  = dataSet?.filter {

                it.bookName!!.contains(query) or
                        it.authorName!!.contains(query) or
                        it.isbn!!.contains(query)

            }?.toTypedArray()
        }
    }
    fun renewDataSet(list:MutableList<AddBook?>){

        if (list.size==0|| isSelect){
            return
        }

        val historyNum = dataSet?.size?:0

        val renewNum = list.size?:0

        val diff = renewNum - historyNum
        if (diff>0){
            SaleBookListAdapter.notifyItemRangeInserted(historyNum, diff)
        }else if (diff == 0){
            SaleBookListAdapter.notifyItemRangeChanged(0, itemCount)
        }else{
            SaleBookListAdapter.notifyItemRangeRemoved(renewNum,diff)
        }


    }

    class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        private val mBinding: ListBookItemBinding? = DataBindingUtil.bind(view)

        fun changeData(addBook: AddBook) {
            if (mBinding != null) {
                mBinding.book = addBook
                if (ClientInfoHolder.ll!=null){
                    val distance =DistanceUtil.getDistance(
                        ClientInfoHolder.ll, LatLng(
                            addBook.position.latitude,
                            addBook.position.longitude
                        )
                    )
                    mBinding.distance =  if(distance>1000) format.format(distance/1000)+"km" else format.format(distance)+"m"
                }else{
                    mBinding.distance = "?m"
                }
                Log.i("BookListAdapter", "changeData")
            }
        }

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_book_item, parent, false)
        return ViewHolder(view)
    }



    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //添加点击事件
        dataSet?.get(position)?.let {
            holder.changeData(it)
        }
        holder.itemView.setOnClickListener {
            if (listener != null) {
                listener!!.onItemClick(holder.itemView, holder.adapterPosition)
            }
        }
    }

    override fun getItemCount(): Int {
        return dataSet?.size?:0
    }



    fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.listener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(itemView: View?, position: Int)
    }
}

